/**
 * 
 * 
 */

/******************************** 自定义 ********************************/
var VKO = {
	statusCode: {ok:200, error:300, timeout:301},
	browserW:document.documentElement.clientWidth,
	browserH:document.documentElement.clientHeight,
	loadLogin:function(){
		$.vkoAjax({
			url:base+"/common/login.jsp",
			data: {url:localUrl},
			success:function(data){
				$.dialog({
					title:"请重新登录",
					lock:true,
					content:data
				});
			}
		});
	},
	ajaxError:function(xhr, ajaxOptions, thrownError){
		$.prompt({content:'网络连接异常',type:'error'});
	},
	ajaxSuccess:function(json){
		if (json===undefined||json==null||json.statusCode === undefined) { 
			return VKO.statusCode.ok;
		} 
		if(json.statusCode == VKO.statusCode.error) {
			return VKO.statusCode.error;
		} else if (json.statusCode == VKO.statusCode.timeout) {
			return VKO.statusCode.timeout;
		} 
		return VKO.statusCode.ok;
	},
	listPage:function(url, data, area, method, add){
		if(!add){
			$(area).empty();
		}
		$.vkoAjax({
			url: basePath + url,
			type: method,
			data: data,
			cache: false,
			success:function(data){
				$.each(data.rows, function(index, row){
					$(area).append(row);
				});
			}
		});	
	},
	strToJson:function(str){
		var data;
		try{
			//data = (new Function("return " + str))();
			//data =  eval("(" + str + ")");
			data = json_parse(str);
		}catch(e){
			data = str;
		}
	    return data;
	},
	ShowLoading:function(){
		$("body").append("<div class='vkoLoading'>加载中，请稍后......</div>")
		$(".vkoLoading").animate({right:0},300);
	},
	HideLoading:function(){
		$(".vkoLoading").animate({right:-200},300,function(){
			$(".vkoLoading").remove();	
		});
	}
}
String.prototype.replaceAll  = function(s1,s2){    
	return this.replace(new RegExp(s1,"g"),s2);    
};

(function($){
	//集体加hoverClass
	$.elementHover = function(str){
		var array = new Array();
		array = str.split(",");
		for(var index in array){
			$(array[index]).hoverClass();
		}
	}
	//拼装页码
	$.createPageHtml = function(pager){
		if(pager.recordCount <2){
			return "";
		}
		var html ="";
		// 1 上一页
		var preHtml ="";
		if(pager.pageNumber == 0){
			pager.pageNumber =1;
		}
		if(pager.pageNumber-1 >0){
			preHtml = " <a  class='firstpage' page='"+(pager.pageNumber-1)+"'>上一页</a>";
		}
		html +=preHtml;
		//第一页
		preHtml = " <a "+(pager.pageNumber==1? "class='current'":"")+" page='1'>" +1+"</a>";
		html +=preHtml;
		//左边....
		if(pager.pageNumber-3 >=2  ){
			preHtml = " ... ";
			html +=preHtml;
		}
		//中间的几页
		var begin = pager.pageNumber-2>2?pager.pageNumber-2:2;
		var end = ( pager.pageNumber+2 < pager.pageCount) ? (pager.pageNumber+2) :((pager.pageCount-1 >1)? (pager.pageCount-1) : 1 );
		for(var i = begin;i <= end;i++){
			preHtml = " <a "+(pager.pageNumber==i? "class='current'":"")+" page='"+(i)+"'>" +i+"</a>";
			html +=preHtml;
		}
		//右边....
		if(pager.pageNumber+2 < pager.pageCount-1  ){
			preHtml = " ... ";
			html +=preHtml;
		}
		//最后一页
		if(pager.pageCount >1 ){
			preHtml = " <a "+(pager.pageNumber==pager.pageCount? "class='current'":"")+" page='"+(pager.pageCount)+"'>" +pager.pageCount+"</a>";
			html +=preHtml;
		}
		//下一页
		if(pager.pageNumber+1 <= pager.pageCount){
			preHtml = " <a  class='lastpage' page='"+(pager.pageNumber+1)+"'>下一页</a>";
			html +=preHtml;
		}
		return html;
	}
	//操作提示动画
	$.vkoFlashTip = function(name){
		/*现有提示：#attSuccess		：关注成功；
					#attCancel		：取消成功；
					#saveSuccess	：收藏成功；
					#appSuccess		：加入购物车成功；
					#appAlready		：已经加入购物车；
					#Success		：操作成功；
		*/
		$("body").append("<div id='"+name+"'></div>");
		var $this = $("#"+name);
		var left = (VKO.browserW - $this.width())*0.5;
		var top = VKO.browserH * 0.4;
		$this.css({left:left,top:top});
		$this.animate({top:top-20,opacity:1},600,function(){
			setTimeout(function(){
				$this.animate({top:top-40,opacity:0},300,function(){
					$this.remove();
				});
			},1000);
		});
	}
	
	$.fn.extend({
		//鼠标经过className替换
		hoverClass: function(className){
			var _className = className || "hover";
			return this.each(function(){
				$(this).hover(function(){
					$(this).addClass(_className);
				},function(){
					$(this).removeClass(_className);
				});
			});
		},
		//回答列表排序
		replyOrderBy: function(options){
			var op = $.extend({
				event: "click",
				wrap:"",
				item:"",
				orderby:""
	    	},options);
			$(this).bind(op.event,function(){
				var itemArr = new Array();
				var list = $(op.wrap);
				
				$(this).addClass("current").siblings().removeClass("current");
				list.children(op.item).each(function(){
					itemArr.push([$(this),$(this).find(op.orderby).html().replace("-","")]);
					$(this).remove();
				});
				//1为升序，2为降序 
				if($(this).attr("order")=="up"){
					var  orderMethod = 1;
					$(this).attr("order","down")
				}else{
					var  orderMethod = 2;
					$(this).attr("order","up")
				}
				itemArr = getOrderArr(itemArr,orderMethod);
				for(var index=0; index<itemArr.length; index++){
					list.append(itemArr[index][0])
				};
			});
			function getOrderArr(array,method){
				for(var i=0; i<array.length; i++)  
		        {  
		            for (var j=array.length-1; j>i; j--)  
		            {  
		                if (method==2)  
		                {  
		                    if (array[i][1] < array[j][1]){
		                    	swap(array,i,j); 
		                    }
		                }else if (method==1)  
		                    if (array[i][1] > array[j][1]){
		                    	 swap(array,i,j);  
		                }
		            }  
		        }  
		        return array;
			}
			function swap(array,i,j){
				var temp = array[i]
				array[i]=array[j];
				array[j]=temp;
			}
		}
	});
})(jQuery);

